let express=require("express");
let bodyParser = require("body-parser");

let db = require("./db");

let app = express();

app.use(express.static("./"));

let dataFormat=bodyParser.urlencoded();

//登录接口
app.get("/login", (req, res) => {
    let {
        phone,
        email,
        password
    } = req.query;

    let data = db.query(
        `SELECT * FROM userlist WHERE phone='${phone}' AND password='${password}'`
    );
    data.then((sqldata) => {
        if (!sqldata[0]) {
            res.send({
                code: false,
                msg: "用户名邮箱或密码错误"
            });
            return;
        }
        res.send({
            code: true,
            msg: "登录成功"
        });
    })
});

//注册接口
app.post("/register", dataFormat, (req, res) => {
    let {
        phone,
        email,
        password,
        username
    } = req.body;
    let data = db.query(
        `SELECT * FROM userlist WHERE phone='${phone}'`
    );

    data.then((sqldata) => {
        if (sqldata[0]) {
            res.send({
                code: false,
                msg: "此号码已存在，请重新输入"
            });
            return;
        }

        db.query(
            `INSERT INTO userlist VALUES('${username}','${phone}','${email}','${password}')`
        ).then((data) => {
            res.send({
                code: true,
                msg: "注册成功"
            });
        })
    })
});

//添加商品到数据库的接口
app.post("/addCar", dataFormat, (req, res) => {
    let {
        userphone,
        goods_id,
        goods_name,
        goods_price,
        goods_num,
        goods_img,
        goods_color,
        goods_size,
        is_select
    } = req.body;

    db.query(
        `SELECT * FROM car WHERE userphone='${userphone}' AND goods_id='${goods_id}'`
    ).then((sqldata) => {
        if (!sqldata[0]) {
            db.query(
                    `INSERT INTO car VALUES('${userphone}','${goods_id}','${goods_name}','${goods_price}','${goods_num}','${goods_img}','${goods_color}','${goods_size}',0)`
                ).then((data) => {
                    res.send({
                        code: true,
                        msg: "添加成功"
                    });
                })
                .catch((err) => {
                    res.send({
                        code: false,
                        msg: "数据添加失败"
                    });
                });
            return;
        }else{
            
        }

        let goodsNum = sqldata[0].goods_num+1;
        let id = sqldata[0].goods_id;
        console.log(sqldata[0].goods_num);

        db.query(
                `UPDATE car SET goods_num='${goodsNum}' WHERE goods_id='${id}'`
            ).then((upd) => {
                res.send({
                    code: true,
                    msg: "添加成功"
                });
            })
            .catch((uperr) => {
                res.send({
                    code: false,
                    msg: "数据添加失败"
                });
            });
    });
});

//获取购物车数据接口
app.get("/getCarData", (req, res) => {
    let {
        userphone
    } = req.query;

    db.query(
        `SELECT * FROM car WHERE userphone='${userphone}'`
    ).then((selectdata) => {
        res.send(selectdata);
        console.log(selectdata);
    });
});

//更新数据库商品数量的接口
app.get("/upCart", (req, res) => {
    let {
        userphone,
        goods_id,
        goods_num
    } = req.query;
    db.query(
            `UPDATE car SET goods_num='${goods_num}' WHERE userphone='${userphone}' AND goods_id='${goods_id}'`
        ).then((sqldata) => {
            res.send({
                code: true,
                msg: "更新成功"
            });
        })
        .catch((err) => {
            res.send({
                code: false,
                msg: "更新失败"
            });
        });
});

//删除商品
app.get("/delectData",(req,res)=>{
    let {userphone,goods_id}=req.query;

    db.query(
        `DELETE FROM car WHERE userphone='${userphone}' AND goods_id='${goods_id}'`
    ).then((sqldata)=>{
        res.send({
            code:true,
            msg:"删除成功"
        })
    })
    .catch((err)=>{
        res.send({
            code:false,
            msg:"删除失败"
        });
    });
});


app.listen(8080,()=>{
    console.log("running port 8080");
});